//
//  JSLBillJiBiController.m
//  JingleSpeedLoan
//
//  Created by 宋志彬 on 2018/8/3.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLBillJiBiController.h"
#import "JSLBillLoanCell.h"
#import "JSLSelectBillProductController.h"
@interface JSLBillJiBiController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalPage;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic , strong) UIView *headerView;
@property (nonatomic , weak) UILabel *moneyLab;
@property (nonatomic , strong) UIView *footerView;
@end

@implementation JSLBillJiBiController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self baseConfig:PageGobackTypeNone];
    [self tableView];
    
    self.tableView.tableHeaderView = self.headerView;
    
    
   
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self refreshData];
//    NSMutableArray *arr =  [CacheManager getObject:@"LOAN_BILL_DATST"];
//
//    self.arrDataSource = [NSMutableArray arrayWithArray:arr];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self.navigationController setNavigationBarHidden:YES];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.navigationController.navigationBar.prefersLargeTitles = NO;
        
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
#pragma mark - 请求数据
- (void)refreshData {
    NSMutableArray *arr =  [CacheManager getObject:@"LOAN_BILL_DATST"];
    
    self.arrDataSource = [NSMutableArray arrayWithArray:arr];
    [self.tableView reloadData];
    
    if (self.arrDataSource.count == 0) {
        self.tableView.tableFooterView = self.footerView;
    }else {
        self.tableView.tableFooterView = nil;
    }
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
    
    
}

- (void)getMoreData {
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return self.arrDataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JSLBillLoanCell *cell = [JSLBillLoanCell cellWithTableView:tableView];
    cell.info = self.arrDataSource[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (indexPath.row == 0) {
//        JSLBillJiBiController *vc = [[JSLBillJiBiController alloc] init];
//        [self pushViewController:vc animated:YES];
//    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10 *AutoSizeScaleYY;
}

#pragma mark - get
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-64 -49 ) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.scrollEnabled = YES;
        _tableView.rowHeight = 86 *AutoSizeScaleYY;
        _tableView.backgroundColor = BACKGROUND_COLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
        
        //        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
        
        _tableView.tableHeaderView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
        
    }
    
    return _tableView;
}

- (UIView *)headerView {
    if (_headerView == nil) {
        _headerView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 210*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
        
       UIImageView *backImageView =  [JSLKitFactory jsl_imageViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180*AutoSizeScaleYY) imageName:@"bill-detial-back" superView:_headerView];
        backImageView.userInteractionEnabled = YES;
        UIImageView *arrwoImage = [JSLKitFactory jsl_imageViewWithFrame:CGRectZero imageName:@"flag-back-icon" superView:backImageView];
        arrwoImage.frame = CGRectMake(10*AutoSizeScaleXX, 38*AutoSizeScaleYY, 14*AutoSizeScaleXX, 14*AutoSizeScaleXX);
        
        CGSize billSize = [@"记一笔" sizeWithAttributes:@{NSFontAttributeName : REGULAR_FONT(17)}];
        [JSLKitFactory jsl_labelWithFrame:CGRectMake(arrwoImage.right +10*AutoSizeScaleXX, 32*AutoSizeScaleYY, billSize.width, billSize.height) text:@"记一笔" font:MEDIUM_FONT(17) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter SuperView:backImageView];
        
        [JSLKitFactory jsl_imageButtonWithFrame:CGRectMake(0, 35*AutoSizeScaleYY, 100*AutoSizeScaleXX, billSize.height +40*AutoSizeScaleYY) superView:backImageView setImage:nil setBackImage:nil target:self action:@selector(backClickAction:)];

//        [JSLKitFactory jsl_imageViewWithFrame:CGRectMake(backImageView.width - 10*AutoSizeScaleXX-40*AutoSizeScaleXX, CGRectGetHeight(backImageView.frame)-20*AutoSizeScaleXX, 40*AutoSizeScaleXX, 40*AutoSizeScaleXX) imageName:@"flag-add-icon" superView:backImageView];
        
        [JSLKitFactory jsl_imageButtonWithFrame:CGRectMake(backImageView.width - 10*AutoSizeScaleXX-40*AutoSizeScaleXX, CGRectGetHeight(backImageView.frame)-20*AutoSizeScaleXX, 40*AutoSizeScaleXX, 40*AutoSizeScaleXX) superView:backImageView setImage:@"flag-add-icon" setBackImage:nil target:self action:@selector(addClickAction:)];
        
        
        
    }
    return _headerView;
}

- (void)backClickAction:(UIButton *)sender {
    [self popViewController];
}

- (void)addClickAction:(UIButton *)sender {
    JSLSelectBillProductController *vc = [[JSLSelectBillProductController alloc] init];
    [self pushViewController:vc animated:YES];
}

- (UIView *)footerView {
    
    if (_footerView == nil) {
        
        _footerView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) backgroundColor:BACKGROUND_COLOR superView:nil];
        
        
        UIImageView *imageView = [JSLKitFactory jsl_imageViewWithFrame:CGRectMake(0, 75*AutoSizeScaleYY, 126*AutoSizeScaleXX, 126*AutoSizeScaleXX) imageName:@"no-info" superView:_footerView];
        imageView.centerX = _footerView.centerX;
        
        CGSize size = [@"暂无数据，快去记一笔吧" sizeWithAttributes:@{NSFontAttributeName : REGULAR_FONT(16)}];
        [JSLKitFactory jsl_labelWithFrame:CGRectMake(0, imageView.bottom +13*AutoSizeScaleYY, _footerView.width, size.height) text:@"亲，暂无数据，快去记一笔吧···" font:REGULAR_FONT(16) textColor:[@"#888888" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:_footerView];
        
        
        //        _footerView.backgroundColor = [UIColor redColor];
        
        
    }
    
    
    return _footerView;
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
